<template>
  <div
    :class="[$route.path === '/home' ? 'article-home' : '']"
    ref="article-item"
  >
    <van-skeleton title avatar :row="3" :loading="skeletonLoading">
      <div v-if="articles">
        <div class="item" v-for="item in  articles" :key="item.id">
        <div class="avatar" v-if="showAvatar" @click="goToUser">
          <van-image
            width="0.9rem"
            height="0.9rem"
            fit="cover"
            round
            :src="item.user.avatar"
          />
          <div class="author">
            <a>{{ item.user.nickname }}</a>
            <span class="introduce">已关注 · {{item.user.introduce}}</span>
          </div>
        </div>
        <div
          class="top"
          v-if="cover.type === 0 || cover.type === 2 || cover.type === 3"
          @click="goToArticle(item.id)"
        >
          <h1>{{ item.title }}</h1>
        </div>
        <div class="centre" v-if="cover.type === 2" @click="goToArticle(item.id)">
          <van-image fit="cover" :src="item.cover_img" />
        </div>
        <div class="centre-3" v-if="cover.type === 3" @click="goToArticle(item.id)">
          <van-image fit="cover" :src="item.cover_img" />
          <van-image fit="cover" :src="item.cover_img" />
          <van-image fit="cover" :src="item.cover_img" />
        </div>
        <div>
          <div class="bottom-1" v-if="cover.type === 1">
            <div class="left">
              <h1 @click="goToArticle(item.id)">{{ item.title }}</h1>
              <div class="left-bottom">
                <div>
                  <span class="author-name" v-if="!showAvatar">{{
                    item.user.nickname
                  }}</span>
                  <span class="comment">{{ item.comment_count }}评论</span>
                  <span class="read">{{ item.read_count }}观看</span>
                  <span class="time">{{ item.createdAt | relativeTime }}</span>
                </div>
                <div>
                  <i class="iconfont icon-cha" @click="onClickCha"></i>
                  <Popover />
                </div>
              </div>
            </div>
            <div class="right" @click="goToArticle(item.id)">
              <van-image
                fit="cover"
                :src="item.cover_img"
              />
            </div>
          </div>
          <div
            class="bottom"
            v-if="cover.type === 0 || cover.type === 2 || cover.type === 3"
          >
            <div>
              <span class="author-name" v-if="!showAvatar">{{
                item.user.nickname
              }}</span>
              <span class="comment">{{ item.comment_count }}评论</span>
              <span class="read">{{ item.read_count }}观看</span>
              <span class="time">{{ item.createdAt | relativeTime }}</span>
            </div>
            <div>
              <i class="iconfont icon-cha" @click="onClickCha"
                ><Popover :placement="'left-end'"
              /></i>
            </div>
          </div>
        </div>
        </div>
      </div>
    </van-skeleton>
    <!-- <div class="item" v-for="item in 2" :key="item">
        <div class="avatar" v-if="showAvatar" @click="goToUser">
          <van-image
            width="33px"
            height="33px"
            fit="cover"
            round
            src="https://img01.yzcdn.cn/vant/cat.jpeg"
          />
          <div class="author">
            <a>你的Maya</a>
            <span>已关注 . 帅哥认证</span>
          </div>
        </div>
        <div
          class="top"
          v-if="cover.type === 0 || cover.type === 2|| cover.type === 3"
          @click="goToArticle(0)"
        >
          <h1>
            数据大屏最简单自适应方案，无需适配rem单位数据大屏最简单自适应方案，无需适配rem单位数据大屏最简单自适应方案，无需适配rem单位数据大屏最简单自适应方案，无需适配rem单位
          </h1>
        </div>
        <div class="centre" v-if="cover.type === 2" @click="goToArticle(0)">
          <van-image fit="cover" src="https://img01.yzcdn.cn/vant/cat.jpeg" />
        </div>
        <div class="centre-3" v-if="cover.type === 3" @click="goToArticle(0)">
          <van-image fit="cover" src="https://img01.yzcdn.cn/vant/cat.jpeg" />
          <van-image fit="cover" src="https://img01.yzcdn.cn/vant/cat.jpeg" />
          <van-image fit="cover" src="https://img01.yzcdn.cn/vant/cat.jpeg" />
        </div>
        <div>
          <div class="bottom-1" v-if="cover.type === 1">
            <div class="left">
              <h1 @click="goToArticle(0)">
                11月上旬这三个板块或将迎来涨停潮数据大屏最简单自适应方案，无需适配rem单位数据大屏最简单自适应方案，无需适配rem单位数据大屏最简单自适应方案，无需适配rem单位
              </h1>
              <div class="left-bottom">
                <div>
                  <span class="author-name" v-if="!showAvatar">你的Maya</span>
                  <span class="comment">945评论</span>
                  <span class="read">945观看</span>
                  <span class="time">{{ teim | relativeTime }}</span>
                </div>
                <div>
                  <i class="iconfont icon-cha" @click="onClickCha"></i>
                  <Popover />
                </div>
              </div>
            </div>
            <div class="right" @click="goToArticle(0)">
              <van-image
                fit="cover"
                src="https://img01.yzcdn.cn/vant/cat.jpeg"
              />
            </div>
          </div>
          <div
            class="bottom"
            v-if="cover.type === 0 || cover.type === 2 || cover.type === 3"
          >
            <div>
              <span class="author-name" v-if="!showAvatar">你的Maya</span>
              <span class="comment">945评论</span>
              <span class="read">945观看</span>
              <span class="time">2小时前</span>
            </div>
            <div>
              <i class="iconfont icon-cha" @click="onClickCha"
                ><Popover :placement="'left-end'"
              /></i>
            </div>
          </div>
        </div>
      </div> -->
  </div>
</template>

<script>
import bus from '@/utils/bus'
// import { getArticles } from '@/api/home'
import { getArticleList } from '@/api/article'
import Popover from '@/components/My-Popover.vue'
import { debounce } from 'lodash'
export default {
  name: 'ArticleItem',
  components: { Popover },
  props: {
    config: {
      zero: false,
      one: false,
      two: false,
      three: false,
      showComment: true
      // showAuthorName: {
      //   type: Boolean,
      //   default: () => true
      // },
      // showComment: {
      //   type: Boolean,
      //   default: true
      // },
      // showAvatar: {
      //   type: Boolean,
      //   default: true
      // },
      // showRead: {
      //   type: Boolean,
      //   default: true
      // },
      // showTime: {
      //   type: Boolean,
      //   default: true
      // }
    },
    data: {
      type: Object,
      required: false // 暂时不必填
    }
  },
  data () {
    return {
      showAvatar: true,
      cover: {
        type: 1, // 封面类型
        images: [] // 图片数组
      },
      teim: '2022-11-04',
      isCha: false,
      loading: false,
      skeletonLoading: true,
      finished: false,
      timestamp: null,
      scrollTop: 0, // 列表滚动到顶部的距离
      articles: [],
      pageNum: 2,
      pageSize: 10,
      total: 0
    }
  },
  watch: {
    articles: function (newVal, oldVal) {
      if (newVal.length >= 0) {
        this.skeletonLoading = false
      }
    },
    data: function (newVal, oldVal) {
      this.articles = newVal.list
      this.skeletonLoading = false
      console.log(newVal);
      console.log(this.articles);
    }
  },
  mounted () {
    const articleItem = this.$refs['article-item']
    articleItem.onscroll = debounce(() => {
      this.scrollTop = articleItem.scrollTop
    }, 50)
  },
  activated () {
    // 把记录的到顶部距离重新设置回去
    this.$refs['article-item'].scrollTop = this.scrollTop
    this.onLoad()
  },
  methods: {
    async onLoad () {
      // this.skeletonLoading = true
      const res = await getArticleList({
        pageNum: this.pageNum,
        pageSize: this.pageSize
      })

      // this.skeletonLoading = false
      this.pageNum = this.pageNum + 1
      this.total = this.total + res.total

      this.articles.push(...res.list)
      // const res = getArticles({
      // channel_id: this.channel.id, // 频道id
      //   timestamp: this.timestamp || Date.now(), // 时间戳，请求新的推荐数据传当前的时间戳，请求历史推荐传指定的时间戳,timestamp相当于页码，请求最新数据使用单前最新时间戳，下一页数据使用上一次返回的数据中的时间戳
      //   with_top: 1 // 是否包含置顶，进入页面第一次请求时要包含置顶文章，1包含，0不包含
      // })

      // setTimeout(() => {
      //   this.articles.push(...res)
      // }, 1000)
      // this.articles.push(...res)
    },
    goToArticle (id) {
      this.$router.push(`/article/${id}`)
    },
    goToUser () {
      this.$router.push('/user')
    },
    onClickCha () {
      console.log('点击了差')
      this.isCha = true
      bus.$emit('onClickCha')
    }
  }
}
</script>
<style lang="scss" scoped>
.article-home {
  position: fixed;
  left: 0;
  right: 0;
  top: 97.95px;
  bottom: 50px;
  overflow-y: auto;
  background-color: rgb(245, 243, 243);
}

.item {
  display: flex;
  flex-direction: column;
  padding: 15px 15px 10px 15px;
  box-sizing: border-box;
  // height:330px;
  margin-top: 10px;
  background-color: #fff;
  .avatar {
    display: flex;
    margin-bottom: 10px;
    .author {
      display: flex;
      flex-direction: column;
      align-items: flex-start;
      margin-left: 10px;
      a {
        font-size: 14px;
        font-weight: 500;
      }
      span {
        font-size: 10px;
        color: #ccc;
      }
      .introduce{
        max-width: 190px;
        overflow: hidden; //溢出内容隐藏
          text-overflow: ellipsis; //文本溢出部分用省略号表示
          display: -webkit-box; //特别显示模式
          -webkit-line-clamp: 1; //行数
          -webkit-box-orient: vertical; //盒子中内容竖直排列
      }
    }
  }
  .top {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    text-align: left;
    h1 {
      font-size: 16px;
      line-height:23px;
      margin: 0;
      overflow: hidden; //溢出内容隐藏
        text-overflow: ellipsis; //文本溢出部分用省略号表示
        display: -webkit-box; //特别显示模式
        -webkit-line-clamp: 2; //行数
        -webkit-box-orient: vertical; //盒子中内容竖直排列
    }
  }
  .centre {
    // border-radius:10px;
    width: 100%;
    height: 200px;
    border-radius: 4px;
        overflow: hidden;
    // display: flex;
    // justify-content: space-between;
    margin-top: 5px;
    .van-image {
      width: 100%;
      height: 200px;
    }
  }
  .centre-3 {
    display: flex;
    justify-content: space-between;
    margin-top: 10px;
    border-radius: 4px;
        overflow: hidden;
    .van-image {
      width: 114px;
      height: 90px;
    }
  }

  .bottom {
    // margin:0;
    // padding:0;
    display: flex;
    justify-content: space-between;
    .icon-cha {
      font-size: 12px;
      color: #ccc;
    }
    span {
      font-size: 12px;
      color: #ccc;
      margin-right: 10px;
    }
  }
  .bottom-1 {
    display: flex;
    box-sizing: border-box;
    height: 90px;
    .left {
      // position: relative;
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      text-align: left;
      width: 230px;
      margin-right: 14px;
      h1 {
        font-size: 16px;
        line-height:23px;
        margin: 0;
        overflow: hidden; //溢出内容隐藏
        text-overflow: ellipsis; //文本溢出部分用省略号表示
        display: -webkit-box; //特别显示模式
        -webkit-line-clamp: 2; //行数
        -webkit-box-orient: vertical; //盒子中内容竖直排列
      }
      .left-bottom {
        display: flex;
        justify-content: space-between;

        .icon-cha {
          font-size: 12px;
          color: #ccc;
        }
        span {
          font-size: 12px;
          color: #ccc;
          margin-right: 10px;
        }
      }
    }
    .right {
      // width: 120px;
      // height: 99px;
      // border-radius: 4px;
      // overflow: hidden;
      .van-image{
        width: 120px;
        height: 85px;
        border-radius: 4px;
        overflow: hidden;
      }
    }
  }
}
</style>
